package com.zhiqianbao.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.LoanApply;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 历史申请记录
 *
 * Created by liang on 2016/2/16.
 */
public class LoanApplyListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<LoanApply> list;
    private Context ctx;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public LoanApplyListAdapter(Activity context, List<LoanApply> list) {
        this.ctx = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    private final class ListItemView {
        TextView textViewApplicationHistoryRecordsTime;
        TextView textViewApplicationHistoryRecordsMoney;
        TextView textViewApplicationHistoryRecordsStatus;
        RelativeLayout historyInfo;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ListItemView listItemView = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.activity_mine_application_history_records_listview_item, null);
            listItemView = new ListItemView();

            listItemView.textViewApplicationHistoryRecordsTime = (TextView) convertView.findViewById(R.id.textView_application_history_records_time);
            listItemView.textViewApplicationHistoryRecordsMoney = (TextView) convertView.findViewById(R.id.textView_application_history_records_money);
            listItemView.textViewApplicationHistoryRecordsStatus = (TextView) convertView.findViewById(R.id.textView_application_history_records_status);
            listItemView.historyInfo = (RelativeLayout) convertView.findViewById(R.id.history_info);

            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }

        LoanApply loanApply = list.get(position);

        String time = sdf.format(new Date(loanApply.getApplyTime()));
        int money = loanApply.getAmount().intValue();
        String state = loanApply.getState();
        //1,4,5,11
        if ("1".equals(state)) {
            listItemView.textViewApplicationHistoryRecordsStatus.setText("用户取消");
            listItemView.textViewApplicationHistoryRecordsStatus.setTextColor(Color.parseColor("#ffdb3c22"));
        } else if ("4".equals(state)) {
            listItemView.textViewApplicationHistoryRecordsStatus.setText("审核拒绝");
            listItemView.textViewApplicationHistoryRecordsStatus.setTextColor(Color.parseColor("#ffdb3c22"));
        } else if ("5".equals(state)) {
            listItemView.textViewApplicationHistoryRecordsStatus.setText("审核取消");
            listItemView.textViewApplicationHistoryRecordsStatus.setTextColor(Color.parseColor("#ffdb3c22"));
        } else if ("11".equals(state)) {
            listItemView.textViewApplicationHistoryRecordsStatus.setText("申请成功");
            listItemView.textViewApplicationHistoryRecordsStatus.setTextColor(Color.parseColor("#ff2af9b5"));
        }


        listItemView.textViewApplicationHistoryRecordsTime.setText(time);
        listItemView.textViewApplicationHistoryRecordsMoney.setText(money + "元");


        return convertView;
    }
}
